package com.cloud.wms.ui.inside.replenish;


import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloud.core.http.BaseObserver;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.ReplenishDownContract;
import com.cloud.wms.ui.inside.contract.ReplenishUpContract;
import com.cloud.wms.ui.inside.entity.ReplenishBindEntity;
import com.cloud.wms.ui.inside.presenter.ReplenishDownPresenter;
import com.cloud.wms.ui.inside.presenter.ReplenishUpPresenter;
import com.cloud.wms.ui.main.activity.LoginActivity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @Author:Chen
 * @Date: 2021-03-01 16:51
 * @Description:
 */
public class ReplenishUpCheckActivity extends BaseBarActivity<ReplenishUpPresenter> implements ReplenishUpContract.View {

    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_box_code)
    TextView tvBoxCode;
    @BindView(R.id.ll_box_info)
    LinearLayout llBoxInfo;
    private TaskEntity taskEntity;
    private ReplenishBindEntity bindInfoEntity;

    @Override
    protected String barTitle() {
        return "检查容器";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_sort_group_bind;
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        mPresenter.checkBindContainer(taskEntity.getBillID());
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.checkBindContainer(taskEntity.getBillID());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        if (result.equalsIgnoreCase(bindInfoEntity.getContainerCode())) {
            ReplenishUpActivity.start(this, bindInfoEntity, taskEntity);
            this.finish();
        } else {
            info("容器扫描检查失败！");
        }
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case ReplenishUpPresenter.CHECK_BIND_CONTAINER:
                bindInfoEntity = ((ReplenishBindEntity) message.obj);
                info("当前任务已绑定容器，请扫描确认！");
                tvBoxCode.setText(bindInfoEntity.getContainerCode());
                llBoxInfo.setVisibility(View.VISIBLE);
                break;
            case ReplenishUpPresenter.JUMP_TO_UP:
                info("补货单未绑定容器，正在跳过检查！");
                ReplenishUpActivity.start(ReplenishUpCheckActivity.this, ((ReplenishBindEntity) message.obj), taskEntity);
                finish();
                break;
        }
    }

    public static void start(Context context, TaskEntity taskEntity) {
        Intent intent = new Intent(context, ReplenishUpCheckActivity.class);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        context.startActivity(intent);
    }
}
